package org.biubiu0002.baiblog.core.controller;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.biubiu0002.baiblog.common.utils.PageUtils;
import org.biubiu0002.baiblog.common.utils.R;
import org.biubiu0002.baiblog.core.annotations.PermissionIgnore;
import org.biubiu0002.baiblog.core.model.vo.ShareVo;
import org.biubiu0002.baiblog.core.model.vo.req.CommentReqVo;
import org.biubiu0002.baiblog.core.model.vo.req.SharePostReqVo;
import org.biubiu0002.baiblog.core.model.vo.res.CommentPostResVo;
import org.biubiu0002.baiblog.core.model.vo.res.ShareBoardItemVo;
import org.biubiu0002.baiblog.core.model.vo.res.SharePostResVo;
import org.biubiu0002.baiblog.core.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 分享内容表
 *
 * @author biubiu0002
 * @email 1007918011@qq.com
 * @date 2022-05-06 00:50:37
 */
@RestController
@RequestMapping("/share")
public class ShareController {
    @Autowired
    private ShareService shareService;

    /**
     * 首页推文列表-分页
     *
     * @param params 分页参数
     * @return 结果
     */
    @GetMapping("/list")
    @PermissionIgnore
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = shareService.queryIndexPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/search")
    @PermissionIgnore
    public R search(@RequestParam Map<String, Object> params) {
        PageUtils page = shareService.searchPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 我发布的推文列表-分页
     *
     * @param params 分页参数
     * @return 结果
     */
    @GetMapping("/my")
    public R myList(@RequestParam Map<String, Object> params) {
        PageUtils page = shareService.queryMyPage(params);
        return R.ok().put("page", page);
    }


    /**
     * ta发布的推文列表-分页
     * @param uid ta的uid
     * @param params 分页参数
     * @return 结果
     */
    @GetMapping("/{uid}/list")
    @PermissionIgnore
    public R shareList(@PathVariable("uid") String uid,
                       @RequestParam Map<String, Object> params) {
        PageUtils page = shareService.queryPage(params,uid);
        return R.ok().put("page",page);
    }

    /**
     * 我收藏的推文列表-分页
     *
     * @param params 分页参数
     * @return 结果
     */
    @GetMapping("/myFavor")
    public R myFavorList(@RequestParam Map<String, Object> params) {
        PageUtils page = shareService.queryMyFavorPage(params);
        return R.ok().put("page", page);

    }

    /**
     * 分享详情
     */
    @GetMapping("/item")
    @PermissionIgnore
    public R info(@RequestParam String shareId) {
        Preconditions.checkArgument(StringUtils.isNotBlank(shareId), "分享id不能为空");
        ShareVo shareVo = shareService.getItemByShareId(shareId);
        return R.ok().put("data", shareVo);
    }

    /**
     * 分享的评论列表-分页
     *
     * @param shareId 分享id
     * @return 分页结果
     */
    @GetMapping("/{shareId}/comments")
    @PermissionIgnore
    public R comments(@PathVariable("shareId") String shareId,
                      @RequestParam Map<String, Object> params) {
        PageUtils page = shareService.queryCommentsPage(shareId, params);
        return R.ok().put("page", page);
    }

    /**
     * 获取某条评论/回复的回复-获取
     *
     * @param commentedId 评论id
     * @return 某条评论/回复的回复
     */
    @GetMapping("/comments/{commentedId}/reply")
    @PermissionIgnore
    public R getReplies(@PathVariable("commentedId") String commentedId,
                        @RequestParam Map<String, Object> params) {
        PageUtils page = shareService.queryRepliesPage(commentedId, params);
        return R.ok().put("page", page);
    }

    /**
     * 发布推文
     *
     * @param sharePostReqVo 发送vo
     * @return 响应结果
     */
    @PostMapping("")
    public R poseShare(@RequestBody SharePostReqVo sharePostReqVo) {
        Preconditions.checkArgument(StringUtils.isNotBlank(sharePostReqVo.getContent()), "发布内容不能为空");
        Preconditions.checkArgument(sharePostReqVo.getContent().length() <= 300, "发布内容不能超过300字");
        Preconditions.checkArgument(sharePostReqVo.getSharePostStatusEnum() != null, "推文发布状态不能为空");
        if (sharePostReqVo.getImageUrls() != null) {
            Preconditions.checkArgument(sharePostReqVo.getImageUrls().size() <= 6, "上传图片不能超过6张");
        }
        SharePostResVo sharePostResVo = shareService.postShare(sharePostReqVo);
        return R.ok().put("data", sharePostResVo);

    }

    /**
     * 修改推文信息
     *
     * @param shareId        推文id
     * @param sharePostReqVo 修改内容
     * @return 修改结果
     */
    @PostMapping("/{shareId}/update")
    public R updateShare(@PathVariable("shareId") String shareId,
                         @RequestBody SharePostReqVo sharePostReqVo) {
        Preconditions.checkArgument(StringUtils.isNotBlank(sharePostReqVo.getContent()), "发布内容不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(shareId), "分享id不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(sharePostReqVo.getContent()) ||
                sharePostReqVo.getSharePostStatusEnum() != null ||
                sharePostReqVo.getImageUrls() != null, "更改信息不能全为空");
        Preconditions.checkArgument(sharePostReqVo.getContent().length() <= 1000, "发布内容不能超过1000字");
        if (sharePostReqVo.getImageUrls() != null) {
            Preconditions.checkArgument(sharePostReqVo.getImageUrls().size() <= 6, "上传图片不能超过6张");
        }

        SharePostResVo sharePostResVo = shareService.updateShare(shareId, sharePostReqVo);
        return R.ok().put("data", sharePostResVo);

    }

    /**
     * 用户删除推文
     *
     * @param shareId 推文id
     */
    @PostMapping("/{shareId}/delete")
    @PermissionIgnore
    public R deleteShare(@PathVariable("shareId") String shareId) {
        shareService.deleteShare(shareId);
        return R.ok();
    }

    /**
     * 点赞推文
     *
     * @param shareId 分享id
     */
    @PostMapping("/{shareId}/like")
    public R likeShare(@PathVariable("shareId") String shareId) {
        shareService.likeShare(shareId);
        return R.ok();
    }

    /**
     * 取消点赞
     *
     * @param shareId 分享id
     */
    @PostMapping("/{shareId}/unlike")
    public R unlikeShare(@PathVariable("shareId") String shareId) {
        shareService.unLikeShare(shareId);
        return R.ok();
    }

    /**
     * 收藏推文
     *
     * @param shareId 分享id
     */
    @PostMapping("/{shareId}/favor")
    public R favorShare(@PathVariable("shareId") String shareId) {
        shareService.favorShare(shareId);
        return R.ok();
    }

    /**
     * 取消收藏推文
     *
     * @param shareId 分享id
     */
    @PostMapping("/{shareId}/unFavor")
    public R unFavorShare(@PathVariable("shareId") String shareId) {
        shareService.unFavorShare(shareId);
        return R.ok();
    }

    /**
     * 评论分享
     *
     * @return ok
     */
    @PostMapping("/{shareId}/comment")
    public R comment(@PathVariable("shareId") String shareId,
                     @RequestBody CommentReqVo commentReqVo) {
        Preconditions.checkArgument(StringUtils.isNotBlank(commentReqVo.getContent()), "评论内容不能为空");
        Preconditions.checkArgument(commentReqVo.getContent().length() <= 100, "评论长度不能超过100字");
        CommentPostResVo comment = shareService.comment(shareId, commentReqVo);
        return R.ok().put("data", comment);
    }

    /**
     * 回复评论
     *
     * @param commentedId  被回复的id
     * @param commentReqVo
     * @return
     */
    @PostMapping("/{commentedId}/reply")
    public R reply(@PathVariable("commentedId") String commentedId,
                   @RequestBody CommentReqVo commentReqVo) {
        Preconditions.checkArgument(StringUtils.isNotBlank(commentReqVo.getContent()), "回复内容不能为空");
        Preconditions.checkArgument(commentReqVo.getContent().length() <= 100, "回复长度不能超过100字");
        CommentPostResVo reply = shareService.reply(commentedId, commentReqVo);
        return R.ok().put("data", reply);
    }

    /**
     * 查看每周点赞榜前十
     *
     * @return 每周点赞榜前十
     */
    @GetMapping("/board/likeCount")
    @PermissionIgnore
    public R likeCountBoard() {
        List<ShareBoardItemVo> shareBoardItemVos = shareService.getLikeCountBoard();
        return R.ok().put("data", shareBoardItemVos);
    }

    /**
     * 查看最新十条推文
     *
     * @return 最新十条推文
     */
    @GetMapping("/board/latest")
    @PermissionIgnore
    public R latestShareBoard() {
        List<ShareBoardItemVo> shareBoardItemVos = shareService.getLatestBoard();
        return R.ok().put("data", shareBoardItemVos);
    }


}
